/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 *//*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.managedbean;

import com.mongodb.DBAddress;
import com.mongodb.MongoClient;
import java.io.IOException;
import java.io.Serializable;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Muharrem Tigdemir <Muharrem Tigdemir at muharremtigdemir.com>
 */
@ManagedBean
@SessionScoped
public class ConnectBean implements Serializable {

    private static final long serialVersionUID = 7863262235394607247L;
    private String userName;
    private String password;
    private String url;
    private MongoClient client;
    private Integer port;
    private boolean isLogin;

    public ConnectBean() {
    }

    @PostConstruct
    public void init() {
        setIsLogin(false);
        setPort(27017);
        setUrl("localhost");
    }

    public boolean checkLogin() {
        System.out.println(client.getConnector().isOpen());
        return client.getConnector().isOpen();
    }

    public void tryConnect() {
        System.out.println(url + "--" + port);
        try {
            client = new MongoClient(url, port);
            client.connect(new DBAddress(url, port, "test"));
            
            System.out.println(client.getConnector().isOpen());

        } catch (UnknownHostException ex) {
            Logger.getLogger(ConnectBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void preRenderView() {
        if (!isLogin) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("/login.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ConnectBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public MongoClient getClient() {
        return client;
    }

    public void setClient(MongoClient client) {
        this.client = client;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isIsLogin() {
        return isLogin;
    }

    public void setIsLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}
